//
//  YHWebVC.m
//  YHRongYiTong
//
//  Created by zxl on 2017/11/20.
//  Copyright © 2017年 YH. All rights reserved.
//

#import "YHWebVC.h"
#import <WebKit/WebKit.h>
#import <JavaScriptCore/JavaScriptCore.h>
#import "WebViewJavascriptBridge.h"
#import "HYNoticeModel.h"


@interface YHWebVC ()

@end

@implementation YHWebVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setup];
}

-(void)setup
{
    [self initData];
    [self createView];
    [self layoutSubViews];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(id)initWithUrl:(NSString *)url withTitle:(NSString *)title
{
    self = [super init];
    if (self) {
        
        self.webView = [[YHWebView alloc] initWithFrame:self.view.bounds withUrl:url];
        self.webView.progressView.hidden = YES;
        YHWeakifySelf;
//        [self.webView setCanGoBackBlock:^(BOOL cangoback) {
//            [yh_weak_self showBackBtn:cangoback];
//        }];
        
        [self.webView setSetTitleBlock:^(NSString *title) {
            yh_weak_self.title = title;
        }];
        
        [self.webView setDidFinishNavigationBlock:^{
            [yh_weak_self actionAfterWebViewDidload];
        }];
        
        self.title = title;
    }
    return self;
}

-(id)initWithHtml:(NSString *)html withTitle:(NSString *)title
{
    self = [super init];
    if (self) {
        
        self.webView = [[YHWebView alloc] initWithFrame:self.view.bounds withHtml:html];
        YHWeakifySelf;
//        [self.webView setCanGoBackBlock:^(BOOL cangoback) {
//            [yh_weak_self showBackBtn:cangoback];
//        }];
        [self.webView setSetTitleBlock:^(NSString *title) {
            yh_weak_self.title = title;
        }];
        self.title = title;
    }
    return self;
}


-(id)initWithNotice:(HYNoticeModel *)model
{
    self = [super init];
    if (self) {
        self.title = @"消息详情";
        
        self.webView = [[YHWebView alloc] init];
        self.webView.hidden = YES;
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 70, VERTICAL_SCREEN_WIDTH, 50)];
        self.titleLabel.font = [UIFont systemFontOfSize:20];
        self.titleLabel.textColor = [UIColor darkGrayColor];
        self.titleLabel.text = model.title;
        
        self.createLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 70+50, VERTICAL_SCREEN_WIDTH, 40)];
        self.createLabel.font = [UIFont systemFontOfSize:15];
        self.createLabel.textColor = [UIColor grayColor];
        self.createLabel.text = [NSString stringWithFormat:@"%@  %@",model.createtime,model.creater];
        
        self.contentTextView = [[UITextView alloc] initWithFrame:CGRectMake(15, 170, VERTICAL_SCREEN_WIDTH, VERTICAL_SCREEN_HEIGHT - 170)];
        self.contentTextView.editable = NO;
        self.contentTextView.layer.borderColor = [UIColor clearColor].CGColor;
        self.contentTextView.backgroundColor = [UIColor clearColor];
        self.contentTextView.textAlignment = NSTextAlignmentLeft;
        NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[model.content dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        
        self.contentTextView.attributedText = attrStr;
        
    }
    return self;
}

-(void)initData
{
    if(!self.webView){
        //默认打开YHDefaultHost
        self.webView = [[YHWebView alloc] initWithFrame:self.view.bounds withUrl:@""];
        self.webView.progressView.hidden = YES;
        YHWeakifySelf;
//      [self.webView setCanGoBackBlock:^(BOOL cangoback) {
//          [yh_weak_self showBackBtn:cangoback];
//      }];
        
        [self.webView setDidFinishNavigationBlock:^{
            [yh_weak_self actionAfterWebViewDidload];
        }];
    }
}

-(void)createView
{
    [self.view addSubview:self.webView];
    
    if (self.titleLabel) {
        
        [self.view addSubview:self.titleLabel];
        [self.view addSubview:self.createLabel];
        [self.view addSubview:self.contentTextView];
    }
}


-(void)layoutSubViews
{
    self.webView.frame = self.view.bounds;
    
    if (self.titleLabel) {
        self.titleLabel.frame = CGRectMake(15, 70, VERTICAL_SCREEN_WIDTH, 50);
    }
    if (self.createLabel) {
        self.createLabel.frame = CGRectMake(15, 70+50, VERTICAL_SCREEN_WIDTH, 40);
    }
    if (self.contentTextView) {
        self.contentTextView.frame = CGRectMake(15, 170, VERTICAL_SCREEN_WIDTH, VERTICAL_SCREEN_HEIGHT - 170);
    }
}

//网页加载完成之后
-(void)actionAfterWebViewDidload
{
    
}

-(void)refresh
{
    [self.webView refresh];
}

#pragma mark lazy loading



#pragma mark webview close btn


-(void)back:(id)Sender
{
    if (self.webView.canGoback) {
        [self.webView goBack];
    }else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

@end
